﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.ESS.APP.App_UI.Forms;
using KPIS.GERP.ESS.APP.App_UI.Commons;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.DAL;
using KPIS.GERP.ESS.MODEL;
using KPIS.GERP.GBMS.BLL;
using _WebShared.Classes;
using Telerik.Web.UI;
using KPIS.DBM;

namespace KPIS.GERP.ESS.APP.App_UI.UserControls
{
    public partial class Mail_BgmRequestTemp : System.Web.UI.UserControl
    {
        Main main = new Main();
        string type = "rq";
        private IProject projectBLL;
        B_HrmEmployees emplist = new B_HrmEmployees();
        B_MessageSend send = new B_MessageSend();
        B_MessageReply reply = new B_MessageReply();
        D_BgmRequestTemp req = new D_BgmRequestTemp();
        B_SECURITY_Users user = new B_SECURITY_Users();
        ComboBLL cmb = new ComboBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            projectBLL = Project_Factory.GetProjectFactory(type);
        }

        public void LoadData(string strBgmReqSeq, string strSender
            , string strApprovePathSeq, string strSendSeq, string strStatus, bool bEdit)
        {
            lblSendSeq.Text = strSendSeq;
            lblApprovePathSeq.Text = strApprovePathSeq;
            lblBgmReqSeq.Text = strBgmReqSeq;

            LoadSenderInfo(strSender);
            //LoadDDLEmpSeq();
            LoadProjectInfo();

            bool bEnable = strStatus == "2";
            cmdApprove.Enabled = !bEnable && bEdit;
            cmdForword.Enabled = !bEnable && bEdit;
            cmdSendBack.Enabled = !bEnable && bEdit;
            PanelOption.Visible = true;
            PanelApprove.Visible = false;
            PanelSendTo.Visible = false;
            PanelSendBack.Visible = false;
            PanelSend.Visible = false;
            txtRemark.Text = "";

            LoadRepeaterBgmSource();
            LoadApprover();
        }

        protected void LoadApprover()
        {
            int nStepNo = 1;
            string strSumAmount = "0";

            if (RepeaterBgmSource.Items.Count > 0)
            {
                strSumAmount = ((Label)RepeaterBgmSource.Controls[RepeaterBgmSource.Controls.Count - 1].Controls[0].FindControl("lblSumTotalAmount")).Text;
            }
            string strNextStepNo = send.GetNextStepNo(lblApprovePathSeq.Text, strSumAmount);

            if (Int32.TryParse(strNextStepNo, out nStepNo))
            {
                if (nStepNo > 0)
                {
                    cmdApprove.Visible = false;
                    cmdForword.Visible = true;

                    // Edit by Aek 2014-07-29
                    // Calculate base on to_emp_seq field in table message_send, not login session 
                    DataTable dtb = send.GetCurrentApprover(null, lblSendSeq.Text, "BGM_REQUEST", nStepNo);

                    if (dtb != null)
                    {
                        if (dtb.Rows.Count > 0)
                        {
                            lblNewApprovePathSeq.Text = dtb.Rows[0]["approve_path_seq"].ToString();
                            //ddlApprover.SelectedValue = dtb.Rows[0]["emp_seq"].ToString();
                            LoadEmpInfo(dtb.Rows[0]["emp_seq"].ToString());
                        }
                    }
                }
                else
                {
                    cmdApprove.Visible = true;
                    cmdForword.Visible = false;
                }
            }
        }

        //protected void SetApproverRight()
        //{
        //    cmdApprove.Visible = user.CheckIsApprover(Session["EmpSeq"].ToString());
        //}

        protected void LoadProjectInfo()
        {
            string strBgmReqSeq = lblBgmReqSeq.Text;
            DataTable dtb = projectBLL.GetDataProjectBySequence(strBgmReqSeq);
            lbBgmYear.Text = dtb.Rows[0]["bgm_year"].ToString();
            lblBgmReqSeq.Text = dtb.Rows[0]["Sequence"].ToString();
            lbProjDate.Text = main.ConvertDBDateTimeToString(dtb.Rows[0]["bgm_prj_date"], true);
            lbProjJobSite.Text = dtb.Rows[0]["job_site_name"].ToString();
            lbProjName.Text = dtb.Rows[0]["bgm_prj_name"].ToString();
            lbProjObject.Text = dtb.Rows[0]["bgm_prj_object"].ToString().Replace("\r\n", "<br />");
            lbProjPlan.Text = dtb.Rows[0]["bgm_prj_plan"].ToString().Replace("\r\n", "<br />");
            lbProjAim.Text = dtb.Rows[0]["bgm_prj_aim"].ToString().Replace("\r\n", "<br />");
            lbProjProfit.Text = dtb.Rows[0]["bgm_prj_profit"].ToString().Replace("\r\n", "<br />");
            lbProjReason.Text = dtb.Rows[0]["bgm_prj_reason"].ToString().Replace("\r\n", "<br />");
            lbProjRemark.Text = dtb.Rows[0]["bgm_prj_remark"].ToString().Replace("\r\n", "<br />");
            lbProjScope.Text = dtb.Rows[0]["bgm_scope_name"].ToString();
            lbProjStrat.Text = dtb.Rows[0]["bgm_strat_list_name"].ToString();
            lbProjTarget.Text = dtb.Rows[0]["bgm_prj_target"].ToString().Replace("\r\n", "<br />");
            lbProjType.Text = dtb.Rows[0]["prj_type_name"].ToString();
            lblStartDate.Text = main.ConvertDBDateTimeToString(dtb.Rows[0]["date_start"], true);
            lblEndDate.Text = main.ConvertDBDateTimeToString(dtb.Rows[0]["date_end"], true);

            DataTable dtb2 = LoadProjectExpense(strBgmReqSeq);

            gridManager.DataSource = projectBLL.GetDataProjectManager(strBgmReqSeq);
            gridManager.DataBind();

            //gridMarket.DataSource = projectBLL.GetDataProjectOption(strBgmReqSeq, "2");
            //gridMarket.DataBind();

            //gridProduct.DataSource = projectBLL.GetDataProjectOption(strBgmReqSeq, "1");
            //gridProduct.DataBind();

            gridProj.DataSource = projectBLL.GetDataProjectOption(strBgmReqSeq, "3");
            gridProj.DataBind();

            string strWarnning = "";

            if (dtb2.Rows.Count == 0)
            {
                strWarnning = "คำเตื่อน!! โครงการนี้ยังไม่มีค่าใช้จ่ายภายในโครงการ";
            }
            //else if (gridManager.Items.Count == 0)
            //{
            //    strWarnning = "คำเตื่อน!! โครงการนี้ยังไม่ได้ระบุผู้รับผิดชอบโครงการ";
            //}
            //else if (gridMarket.Items.Count == 0)
            //{
            //    strWarnning = "คำเตื่อน!! โครงการนี้ยังไม่ได้ระบุตลาด/ประเทศ";
            //}
            //else if (gridProduct.Items.Count == 0)
            //{
            //    strWarnning = "คำเตื่อน!! โครงการนี้ยังไม่ได้ระบุสินค้า/บริการ";
            //}
            //else if (gridProj.Items.Count == 0)
            //{
            //    strWarnning = "คำเตื่อน!! โครงการนี้ยังไม่ได้ระบุโครงการบูรณาการ";
            //}

            lblError.Text = strWarnning;
        }

        protected DataTable LoadProjectExpense(string strBgmReqSeq)
        {
            IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
            dbManager.CreateParameters(2);
            dbManager.AddParameters(0, "@RecordStatus", SystemInfo._ActiveRecordStatus);
            dbManager.AddParameters(1, "@seq", strBgmReqSeq);

            string strSQL = "select e.bgm_exp_seq as value"
                + ", concat(e.bgm_exp_name"
                    + ", case when re.icm_res_item_seq is not null"
                        + " then concat('/', coalesce(item.icm_res_item_name, ''))"
                        + " else ''"
                        + " end) as text"
                + ", re.total_amount"
                + ", re.bgm_req_exp_seq as bgm_proj_exp_seq"
                + ", re.bgm_source_seq, s.bgm_source_name"
                + ", eg.bgm_exp_grp_seq, eg.bgm_exp_grp_name, eg.bgm_exp_grp_x"
                + ", e1.bgm_exp_seq as bgm_exp_seq_1, e1.bgm_exp_name as bgm_exp_name_1"
                + ", e2.bgm_exp_seq as bgm_exp_seq_2, e2.bgm_exp_name as bgm_exp_name_2"
                + ", re.unit_price ,re.unit_qty ,re.descr"
                + ", re.icm_res_item_seq, re.unit_price, re.unit_qty"
                + " from bgm_mas_expense e"
                + " left join bgm_mas_expense_group eg on e.bgm_exp_grp_seq = eg.bgm_exp_grp_seq"
                + " left join bgm_mas_expense e1 on e1.bgm_exp_seq = e.upper_bgm_exp_seq"
                + " left join bgm_mas_expense e2 on e2.bgm_exp_seq = e1.upper_bgm_exp_seq"
                + " left join bgm_request_expense re on re.bgm_exp_seq = e.bgm_exp_seq"
                    + " and re.bgm_req_seq = @seq"
                + " left join bgm_mas_source s on re.bgm_source_seq = s.bgm_source_seq"
                + " left join icm_mas_resource_item item on re.icm_res_item_seq = item.icm_res_item_seq"

                + " where e.bgm_exp_seq not in (select upper_bgm_exp_seq"
                        + " from bgm_mas_expense"
                        + " where record_status = 'A'"
                        + " and upper_bgm_exp_seq is not null)"
                    + " and re.bgm_req_seq = @seq"
                //+ " group by e.bgm_exp_seq, e.bgm_exp_name, eg.bgm_exp_grp_seq, eg.bgm_exp_grp_code, eg.bgm_exp_grp_name"
                //    + ", e1.bgm_exp_seq, e1.bgm_exp_name, e2.bgm_exp_seq, e2.bgm_exp_name"
                + " order by eg.bgm_exp_grp_code, e.bgm_exp_name";
            //string strSQL = "select e.bgm_exp_seq as value, e.bgm_exp_name as text"
            //    + ", sum(coalesce(re.total_amount, 0)) as total_amount"
            //    + ", re.bgm_source_seq, s.bgm_source_name, re.descr"
            //    + ", eg.bgm_exp_grp_seq, eg.bgm_exp_grp_name, eg.bgm_exp_grp_x"
            //    + ", e1.bgm_exp_seq as bgm_exp_seq_1, e1.bgm_exp_name as bgm_exp_name_1"
            //    + ", e2.bgm_exp_seq as bgm_exp_seq_2, e2.bgm_exp_name as bgm_exp_name_2"

            //    + " from bgm_mas_expense e"
            //    + " left join bgm_mas_expense_group eg on e.bgm_exp_grp_seq = eg.bgm_exp_grp_seq"
            //    + " left join bgm_mas_expense e1 on e1.bgm_exp_seq = e.upper_bgm_exp_seq"
            //    + " left join bgm_mas_expense e2 on e2.bgm_exp_seq = e1.upper_bgm_exp_seq"
            //    + " left join (select re.bgm_req_seq, re.bgm_exp_seq, re.bgm_source_seq, re.descr"
            //        + ", sum(coalesce(re.total_amount, 0)) as total_amount"
            //        + " from bgm_request_expense re"
            //        + " group by re.bgm_req_seq, re.bgm_exp_seq, re.bgm_source_seq"
            //        + ") re on re.bgm_exp_seq = e.bgm_exp_seq"
            //        + " and re.bgm_req_seq = @seq"
            //    + " left join bgm_mas_source s on re.bgm_source_seq = s.bgm_source_seq"

            //    + " where e.bgm_exp_seq not in (select upper_bgm_exp_seq"
            //        + " from bgm_mas_expense"
            //        + " where record_status = 'A'"
            //        + " and upper_bgm_exp_seq is not null)"
            //    + " and re.bgm_req_seq = @seq"
            //    + " group by e.bgm_exp_seq, e.bgm_exp_name"
            //        + ", re.bgm_source_seq, s.bgm_source_name, re.descr"
            //        + ", eg.bgm_exp_grp_seq, eg.bgm_exp_grp_name, eg.bgm_exp_grp_x"
            //        + ", e1.bgm_exp_seq, e1.bgm_exp_name"
            //        + ", e2.bgm_exp_seq, e2.bgm_exp_name"
            //    + " order by eg.bgm_exp_grp_code, e.bgm_exp_name";
            dbManager.Open();
            DataTable dtb2 = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();

            gridExpense.DataSource = dtb2;
            gridExpense.DataBind();

            return dtb2;
        }

        protected void LoadRepeaterBgmSource()
        {
            string strBgmReqSeq = lblBgmReqSeq.Text;

            IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
            dbManager.CreateParameters(2);
            dbManager.AddParameters(0, "@RecordStatus", SystemInfo._ActiveRecordStatus);
            dbManager.AddParameters(1, "@seq", strBgmReqSeq);

            string strSQL = "select s.bgm_source_name"
                + ", sum(coalesce(re.total_amount, 0)) as total_amount"
                + ", s.bgm_source_seq"
                + " from bgm_request_expense re"
                + " left join bgm_mas_source s on re.bgm_source_seq = s.bgm_source_seq"

                + " where re.bgm_req_seq = @seq"
                + " group by s.bgm_source_name, s.bgm_source_seq"
                + " order by s.bgm_source_seq";
            dbManager.Open();
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];

            RepeaterBgmSource.DataSource = dtb;
            RepeaterBgmSource.DataBind();

            dbManager.Dispose();

            PanelTempBgmSource.Visible = false;
            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                if (!CheckBgmSourceLeafNode(dtb.Rows[i]["bgm_source_seq"].ToString()))
                {
                    PanelTempBgmSource.Visible = true;
                    ddlBgmSourceSeq.DataSource = cmb.ListSourceCombo();
                    ddlBgmSourceSeq.DataBind();
                    return;
                }
            }
        }

        protected bool CheckBgmSourceLeafNode(string strBgmSourceSeq)
        {
            bool bLeaf = false;

            IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
            dbManager.CreateParameters(2);
            dbManager.AddParameters(0, "@DeleteStatus", SystemInfo._DeleteRecordStatus);
            dbManager.AddParameters(1, "@seq", strBgmSourceSeq);

            string strSQL = "select count(*) as child_count"
                + " from bgm_mas_source s"

                + " where s.record_status <> @DeleteStatus"
                + " and s.upper_bgm_source_seq = @seq";
            dbManager.Open();
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            bLeaf = Convert.ToInt32(dtb.Rows[0][0]) == 0;

            return bLeaf;
        }

        protected void RepeaterBgmSource_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item is RepeaterItem)
            {
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    string strBgmReqSeq = lblBgmReqSeq.Text;

                    IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
                    dbManager.CreateParameters(2);
                    dbManager.AddParameters(0, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                    dbManager.AddParameters(1, "@seq", strBgmReqSeq);

                    dbManager.Open();
                    string strSQL = "select sum(coalesce(re.total_amount, 0)) as total_amount"
                        + " from bgm_request_expense re"

                        + " where re.bgm_req_seq = @seq"
                        + " group by re.bgm_req_seq";
                    DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                    if (dtb.Rows.Count > 0)
                    {
                        ((Label)e.Item.FindControl("lblSumTotalAmount")).Text = Convert.ToDouble(dtb.Rows[0][0]).ToString("#,###.00");
                    }
                    dbManager.Dispose();
                }
            }
        }

        protected void LoadSenderInfo(string strEmpSeq)
        {
            DataTable dtb = emplist.GetInfo(Convert.ToInt32(strEmpSeq));

            if (dtb.Rows.Count > 0)
            {
                lblEmpSeq.Text = strEmpSeq;
                lblEmpName.Text = dtb.Rows[0]["full_name"].ToString();
                lblPosName.Text = dtb.Rows[0]["pos_name"].ToString();
                lblClassName.Text = dtb.Rows[0]["class_name"].ToString();
                lblJobsiteName.Text = dtb.Rows[0]["job_site_name"].ToString();
            }
        }

        public void cmdApprove_Click(object sender, EventArgs e)
        {
            //string strNextSeqNo = send.GetNextStepNo(lblApprovePathSeq.Text);

            //if (strNextSeqNo == "0")
            //{
            //    PanelApprove.Visible = true;
            //    PanelSendTo.Visible = false;
            //    lblMode.Text = "A";
            //}
            //else
            //{
            //    DataTable dtb = send.GetCurrentApprover(Session["EmpSeq"].ToString(), "bgm_request"
            //        , Convert.ToInt32(strNextSeqNo));

            //    if (dtb != null)
            //    {
            //        if (dtb.Rows.Count > 0)
            //        {
            //            lblNewApprovePathSeq.Text = dtb.Rows[0]["approve_path_seq"].ToString();
            //            LoadEmpInfo(dtb.Rows[0]["emp_seq"].ToString());
            //        }
            //    }

            //    PanelApprove.Visible = false;
            //    PanelSendTo.Visible = true;
            //    lblMode.Text = "F";
            //}

            PanelSendBack.Visible = true;
            // button
            PanelOption.Visible = false;
            PanelSend.Visible = true;
            lblMode.Text = "A";
        }

        protected void cmdForword_Click(object sender, EventArgs e)
        {
            PanelApprove.Visible = false;
            PanelSendTo.Visible = true;
            lblMode.Text = "F";

            PanelSendBack.Visible = true;
            // button
            PanelOption.Visible = false;
            PanelSend.Visible = true;
        }

        protected void cmdSendBack_Click(object sender, EventArgs e)
        {
            lblMode.Text = "B";

            PanelApprove.Visible = false;
            PanelSendTo.Visible = false;
            PanelSendBack.Visible = true;
            // button
            PanelOption.Visible = false;
            PanelSend.Visible = true;
            lblError.Text = "";
        }

        protected void LoadEmpInfo(string strEmpSeq)
        {
            if (strEmpSeq.Length > 0)
            {
                DataTable dtb = emplist.GetInfo(Convert.ToInt32(strEmpSeq));

                lblApproverEmpSeq.Text = dtb.Rows[0]["sequence"].ToString();
                ddlApprover.Text = dtb.Rows[0]["full_name"].ToString();
                lblApproverPosName.Text = dtb.Rows[0]["pos_name"].ToString();
                lblApproverClassName.Text = dtb.Rows[0]["class_name"].ToString();
                lblApproverExecPosName.Text = dtb.Rows[0]["exec_pos_name"].ToString();
                lblApproverJobsiteName.Text = dtb.Rows[0]["job_site_name"].ToString();
            }
        }

        private bool directApprove = false;
        public string Approve(string remark)
        {
            cmdApprove_Click(new Object(), new EventArgs());
            txtRemark.Text = remark;
            return cmdSendClick();
        }

        public void cmdSend_Click(object sender, EventArgs e)
        {
            cmdSendClick();
        }

        public string cmdSendClick()
        {
            try
            {
                int nEmpSeqLogin = SsMng.EmpSeqI;
                ESS.MODEL.M_MessageSend info = new ESS.MODEL.M_MessageSend();
                ESS.MODEL.M_MessageReply replyInfo = new ESS.MODEL.M_MessageReply();
                // By Aek 2014-07-29
                // remark: in forwarding FromEmpSeq should be the previous emp_seq in message_send
                // string strFromEmpSeq = Session["EmpSeq"].ToString();
                var messageSendPrev = send.GetFull(null, null, null, null, null, null, null, lblSendSeq.Text)[0];
                string strFromEmpSeq = messageSendPrev.to_emp_seq.ToString();
                string strFromPosSeq = messageSendPrev.to_pos_seq.ToString();

                int nFromEmpSeq = Convert.ToInt32(strFromEmpSeq);
                int nFromPosSeq = Convert.ToInt32(strFromPosSeq);

                Nullable<int> nToEmpSeq = null;
                Nullable<int> nToPosSeq = null;
                Nullable<int> nWodEmpSeq = nEmpSeqLogin == nFromEmpSeq ? (int?)null : nEmpSeqLogin;

                bool bLeaf = true;
                string strSendSeq = lblSendSeq.Text;
                string strToEmpSeq = lblApproverEmpSeq.Text;

                if (strToEmpSeq.Length == 0 && ddlApprover.Visible)
                {
                    lblError.Text = "คำเตือน!! กรุณาเลือกผู้อนุมัติก่อนส่ง";
                    return lblError.Text;
                }
                else
                {
                    lblError.Text = "";
                }

                while (bLeaf)
                {
                    ESS.MODEL.M_MessageSend sendInfo = send.GetInfo(strSendSeq);
                    string strFromSendSeq = sendInfo.from_send_seq.ToString();

                    if (string.IsNullOrEmpty(strFromSendSeq))
                    {
                        bLeaf = false;
                        nToEmpSeq = sendInfo.from_emp_seq;
                        nToPosSeq = send.GetPosSeqFromEmpSeq(Convert.ToString(nToEmpSeq));
                    }

                    strSendSeq = strFromSendSeq;
                }

                switch (lblMode.Text)
                {
                    case "A":
                        if (main.CheckDuplicateMessage2(lblSendSeq.Text))
                        {
                            main.MessageAlert(Page, "รายการนี้อนุมัติไปแล้ว กรุณาอย่ากดซ้ำ");
                            return "";
                        }

                        info.send_seq = Convert.ToInt32(lblSendSeq.Text);
                        info.message_status = "2";
                        info.wod_emp_seq = nWodEmpSeq;
                        info.updated_by = nEmpSeqLogin;
                        info.updated_when = DateTime.Now;
                        send.UpdateMessageStatus(info);

                        replyInfo.reply_date = DateTime.Today;
                        replyInfo.ref_no = send.GetRefNoOfSendSeq(Convert.ToInt32(lblSendSeq.Text));
                        replyInfo.reply_type = "1";
                        replyInfo.reply_text = "อนุมัติคำของบประมาณ : " + lbProjName.Text;
                        replyInfo.from_emp_seq = nFromEmpSeq;
                        replyInfo.from_pos_seq = nFromPosSeq;
                        replyInfo.to_emp_seq = nToEmpSeq;
                        replyInfo.to_pos_seq = nToPosSeq;
                        replyInfo.wod_emp_seq = nWodEmpSeq;
                        replyInfo.from_send_seq = Convert.ToInt32(lblSendSeq.Text);
                        replyInfo.message_status = "0";
                        replyInfo.created_by = nEmpSeqLogin;
                        replyInfo.created_when = DateTime.Now;
                        replyInfo.remarks = txtRemark.Text;
                        reply.Insert(replyInfo);

                        req.ChangeStatus(Convert.ToInt32(lblBgmReqSeq.Text), "A");
                        req.PortBgmRequestToBgmProject(Convert.ToInt32(lblBgmReqSeq.Text));
                        //============== alt system ==============
                        req.PortSystem1ToSystem2(Convert.ToInt32(lblBgmReqSeq.Text));
                        break;
                    case "F":
                        if (main.CheckDuplicateMessage(lblSendSeq.Text))
                        {
                            main.MessageAlert(Page, "รายการนี้ส่งไปแล้ว กรุณาอย่ากดซ้ำ");
                            return "";
                        }

                        info.send_date = DateTime.Today;
                        info.ref_seq = main.ConvertNullInt(lblBgmReqSeq.Text);
                        info.ref_type = "bgm_request";
                        info.ref_no = send.GetRefNoOfSendSeq(Convert.ToInt32(lblSendSeq.Text));
                        info.send_text = "ส่งคำของบประมาณ : " + lbProjName.Text;
                        info.from_emp_seq = nFromEmpSeq;
                        info.from_pos_seq = nFromPosSeq;
                        info.to_emp_seq = main.ConvertNullInt(strToEmpSeq);
                        info.to_pos_seq = send.GetPosSeqFromEmpSeq(strToEmpSeq);
                        info.from_send_seq = main.ConvertNullInt(lblSendSeq.Text);
                        info.remarks = txtRemark.Text;
                        info.message_status = "0";
                        info.approve_path_seq = main.ConvertNullInt(lblNewApprovePathSeq.Text);
                        info.created_by = nEmpSeqLogin;
                        info.created_when = DateTime.Now;
                        send.Insert(info);

                        info.send_seq = Convert.ToInt32(lblSendSeq.Text);
                        info.message_status = "2";
                        info.wod_emp_seq = nWodEmpSeq;
                        info.updated_by = nEmpSeqLogin;
                        info.updated_when = DateTime.Now;
                        send.UpdateMessageStatus(info);
                        break;
                    case "B":
                        //projectBLL.ApproveProject(lblBgmReqSeq.Text, "W");

                        info.send_date = DateTime.Today;
                        info.ref_seq = main.ConvertNullInt(lblBgmReqSeq.Text);
                        info.ref_type = "return";
                        info.ref_no = send.GetRefNoOfSendSeq(Convert.ToInt32(lblSendSeq.Text));
                        info.send_text = "ส่งกลับเพื่อแก้ไข : " + lbProjName.Text;
                        info.from_emp_seq = nFromEmpSeq;
                        info.from_pos_seq = nFromPosSeq;
                        info.to_emp_seq = nToEmpSeq;
                        info.to_pos_seq = nToPosSeq;
                        info.from_send_seq = main.ConvertNullInt(lblSendSeq.Text);
                        info.remarks = txtRemark.Text;
                        info.message_status = "0";
                        info.approve_path_seq = null;//main.ConvertNullInt(lblNewApprovePathSeq.Text);
                        info.created_by = nEmpSeqLogin;
                        info.created_when = DateTime.Now;
                        send.Insert(info);

                        replyInfo.reply_date = DateTime.Today;
                        replyInfo.ref_no = send.GetRefNoOfSendSeq(Convert.ToInt32(lblSendSeq.Text));
                        replyInfo.reply_type = "0";
                        replyInfo.reply_text = "ส่งกลับมาเพื่อแก้ไข : " + lbProjName.Text;
                        replyInfo.from_emp_seq = nFromEmpSeq;
                        replyInfo.from_pos_seq = nFromPosSeq;
                        replyInfo.to_emp_seq = nToEmpSeq;
                        replyInfo.to_pos_seq = nToPosSeq;
                        replyInfo.wod_emp_seq = nWodEmpSeq;
                        replyInfo.from_send_seq = Convert.ToInt32(lblSendSeq.Text);
                        replyInfo.remarks = txtRemark.Text;
                        replyInfo.message_status = "0";
                        replyInfo.created_by = nEmpSeqLogin;
                        replyInfo.created_when = DateTime.Now;
                        reply.Insert(replyInfo);

                        info.send_seq = Convert.ToInt32(lblSendSeq.Text);
                        info.message_status = "2";
                        info.wod_emp_seq = nWodEmpSeq;
                        info.updated_by = nEmpSeqLogin;
                        info.updated_when = DateTime.Now;
                        send.UpdateMessageStatus(info);

                        req.ChangeStatus(Convert.ToInt32(lblBgmReqSeq.Text), "R");
                        break;
                }

                ((TaskListsPopUp)Page).cmdViewRequest_Click(null, null);
            }
            catch (Exception ex)
            {
                string errorStr = ex.Message + " " + ex.InnerException;
                if (!directApprove) main.MessageAlert(Page, errorStr);
                return errorStr;
            }
            return "";
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            PanelApprove.Visible = false;
            PanelSendTo.Visible = false;
            PanelSendBack.Visible = false;
            // button
            PanelOption.Visible = true;
            PanelSend.Visible = false;
        }

        protected void ddlApprover_DataBound(object sender, EventArgs e)
        {
            //set the initial footer label
            ((Literal)ddlApprover.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(ddlApprover.Items.Count);
        }

        protected void ddlApprover_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            ddlApprover.DataSource = emplist.ListEmployeeByType("('1', '2', '3')", "");
            ddlApprover.DataBind();
        }

        protected void ddlApprover_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = ((DataRowView)e.Item.DataItem)["full_name"].ToString();
            e.Item.Value = ((DataRowView)e.Item.DataItem)["sequence"].ToString();
        }

        protected void ddlApprover_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadEmpInfo(ddlApprover.SelectedValue);
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (ddlBgmSourceSeq.SelectedValue.Length > 0)
            {
                IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
                dbManager.CreateParameters(2);
                dbManager.AddParameters(0, "@bgm_source_seq", ddlBgmSourceSeq.SelectedValue);
                dbManager.AddParameters(1, "@seq", lblBgmReqSeq.Text);

                string strSQL = "update bgm_request_expense"
                    + " set bgm_source_seq = @bgm_source_seq"

                    + " where bgm_req_seq = @seq";
                dbManager.Open();
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.Dispose();

                LoadProjectExpense(lblBgmReqSeq.Text);
                LoadRepeaterBgmSource();
            }
        }
    }
}